const readFile = require("../../../utils/readFile");
const mkdirSync = require("../../../utils/mkdirSync");

function upPathData(req, res, next) {
  const { url, data } = req.body;
  if (url && data) {
    readFile(url).then(
      () => {
        console.log("路径已有 请重新分配路径");
        res.json({
          code: 10000,
          data: {},
          message: "路径已存在 请重新分配路径 创建json文件失败",
        });
      },
      () => {
        mkdirSync(url, data).then(
          (mess) => {
            res.json({
              code: 200,
              data: mess,
              message: "添加数据成功",
            });
          },
          (mess) => {
            res.json({
              code: 10000,
              data: {},
              message: mess,
            });
          }
        );
      }
    );
  } else {
    console.log("填写url &&  data");
    res.json({
      code: 10000,
      data: {},
      message: "填写url &&  data",
    });
  }
}

module.exports = {
  path: "/upPathData",
  function: upPathData,
  method: "post",
};
